import { createStore, combineReducers, applyMiddleware, AnyAction, Middleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import createSagaMiddleware from 'redux-saga'
import rootSaga from '@redux/saga/rootSaga'
import homeReducer, { HomeStore } from '@redux/reducer/homeReducer'
const sagaMiddleware = createSagaMiddleware()

export interface RootState {
  home: HomeStore
}

const bindMiddleware = (middleware: Middleware[]) => {
  if (process.env.NODE_ENV !== 'production') {
    return composeWithDevTools(applyMiddleware(...middleware))
  }
  return applyMiddleware(...middleware)
}

const store = createStore(
  combineReducers<RootState, AnyAction>({
    home: homeReducer
  }),
  bindMiddleware([sagaMiddleware])
)
sagaMiddleware.run(rootSaga)
export default store
